package com.itheima.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.QuoteException;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import com.itheima.vo.OrderSettingVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;

    //查询所有的预约数据
    @Override
    public List<OrderSettingVo> findByMonth(String month) {
        if (month == null || !month.contains("-")) {
            throw new QuoteException();
        }
       /* String startDate = month + "-1"; //设置月初
        String endDate = month + "-31";//设置月末*/
        Date startDate= null;
        Date endDate= null;
        try {
            startDate = DateUtils.parseString2Date(month + "-1");
            endDate = DateUtils.parseString2Date(month + "-31");
        } catch (Exception e) {
          throw new QuoteException("date类型解析失败");
        }
        //查询所有的预约设置数据
        List<OrderSetting> orderSettingList = orderSettingDao.getOrderSettingByMonth(startDate, endDate);

        if (orderSettingList != null && orderSettingList.size() > 0) {
            List<OrderSettingVo> data = new ArrayList<>();
            for (OrderSetting orderSetting : orderSettingList) {
                OrderSettingVo orderSettingVo = new OrderSettingVo();
                Calendar calendar=Calendar.getInstance();
                calendar.setTime(orderSetting.getOrderDate());
                orderSettingVo.setDate(String.valueOf(calendar.get(Calendar.DAY_OF_MONTH))); //获取日期
                orderSettingVo.setNumber(orderSetting.getNumber());//获取可预约人数
                orderSettingVo.setReservations(orderSetting.getReservations());//已预约人数
                data.add(orderSettingVo);
            }
            return data;
        } else {
            return null;
        }
    }

    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                OrderSetting orderDate = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
                if (orderDate == null) {
                    orderSettingDao.add(orderSetting);
                }
                orderSettingDao.editNumberByOrderDate(orderSetting);
            }
            /* for (OrderSetting orderSetting : list) {
                //判断是否有预约数据
                long count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //存在更新
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    //不存在就执行添加
                    orderSettingDao.add(orderSetting);
                }

            }*/
        }


    }


    //设置某一天的可预约数据
    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        if (orderSetting == null) {
            throw new QuoteException();
        }
        //预约设置,如果已存在,就执行更新.如果没有存在,就执行添加
        //通过预约日期查询当前预约数据是否已存在
        OrderSetting orderDate = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
        if (orderDate != null) {
            //已存在,则执行更新
            orderSettingDao.editNumberByOrderDate(orderSetting);
        } else {
            //当前日期没有进行预约设置,进行添加操作
            orderSettingDao.add(orderSetting);
        }
    }
}
